/**
 * 
 */
package wong.board.client.widgets;

import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.user.client.ui.HasValue;

/**
 * @author wong
 * 
 */
public class ToggleButton extends Button<Boolean> implements HasValue<Boolean> {

	public ToggleButton(String text) {
		super(Boolean.FALSE, text);
		addStyleName(css.buttonToggle());
	}

	@Override
	public void setValue(Boolean value, boolean fireEvents) {
		if (value != null && this.getValue() != null && !value.equals(this.getValue())) {
			if (this.getValue().booleanValue()) {
				this.removeStyleName(css.buttonToggleSelected());
			} else {
				this.addStyleName(css.buttonToggleSelected());
			}
		}
		super.setValue(value, fireEvents);
	}

	protected void toggle() {
		setValue(Boolean.valueOf(!getValue().booleanValue()));
	}

	@Override
	protected void callAction(DomEvent<?> event) {
		toggle();
		super.callAction(event);
	}
}
